package com.dao;

import com.bean.Shop;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SomeFun {
    public List<Shop> selectlike(String x) throws ClassNotFoundException, SQLException {
        String username = "root";
        String password = "123456";
//        String url = "jdbc:mysql://localhost:3306/run?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false";
        String url = "mysql://localhost:3306/run?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=utf8&useSSL=false";

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection(url,username, password);

        String sql = "select * from shop where name like ? ";
        PreparedStatement ps = con.prepareStatement(sql) ;
        ps.setString(1,"%"+x+"%");

        ResultSet resultSet = ps.executeQuery();
        List<Shop> shops = new ArrayList<>();
        while (resultSet.next()){
            Shop shop = new Shop();
            shop.setId(resultSet.getInt(1));
            shop.setName(resultSet.getString(2));
            shop.setPrice(resultSet.getInt(3));
            shops.add(shop);
        }

        resultSet.close();
        ps.close();
        con.close();

        return shops;
    }
}
